//////////////////////////////////////////////////////////////////////////
//                                                                      //
// xrddm                                                                //
//                                                                      //
// Author: Martin Vala (SASKE Slovakia and JINR, Russia, 2010)          //
//                                                                      //
// A xrdcp-like command line tool for copying from alien                //
// and other sources                                                    //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#ifndef __XrdDownloadManagerBase_H__
#define __XrdDownloadManagerBase_H__

#include <string>
#include <vector>
using namespace std;

#include <XrddmConfig.h>
#include <XrdDownloadManagerLog.hh>

class XrdDownloadManagerBase {

public:

    enum EInputTypes_t { kStdXrdcp, kStdXrdcpGR, kStdXrdcpAliEnWhereis, kStdXrdcpAliEnWhereisGR, kStdAliEnCp, kStdRootMacroCp };
    
    XrdDownloadManagerBase();
    ~XrdDownloadManagerBase();

    virtual int DoAuthenticate ( bool showProcess );
    virtual int ReadConfigFile ( string filename );
    int DoCopy ( string from, string to );
    virtual int DoCopyPre ( string from, string to );
    virtual int DoCopyWithType ( EInputTypes_t type, string from, string to );
    virtual int DoCopyPost ( string from, string to );

    virtual int DoCopyStd ( string from, string to );
    virtual int DoCopyStdGR ( string from, string to );
    virtual int DoCopyStdROOT ( string from, string to );

    int           DoXrdCp ( string from, string to );
    void          DoXrdcpInLogFile ( bool b = true, int debugLevel = 0,string inputfile="mylog.log");


    void          SetLogDirectory ( string dir ) { fLogDirectory=dir; }
    void          SetGlobalLogFile ( string file ) { fCurrentGlobalLogFileName=file; }
    string          XrdCpCmd() { return fXrdcpCmd; }
    void          UseAuthentication ( bool b=true ) { fDoAuthenticate = b; };

    void          SetXrdcpOptions ( string opts ) { fXrdcpCmdOptions = opts; }
    void          SetXrdcpCmdCn ( string opts ) { fXrdcpCmdCn = opts; }
    void          SetXrdcpCmdRootMacro ( string opts ) { fXrdcpCmdRootMacro = opts; }
    
    void          SetSourceTypes ( string type ) { fSourceTypes = type; }
    void          SetGlobalRedirectorName ( string type ) { fGlobalRedirectorName = type; }
    void          UseRemoveLogAfterSuccessCopy ( bool b=true ) { fRemoveLogAfterSuccessCopy = b; };
    void          UseRemoveLogDirectoryAfterSuccessCopy ( bool b=true ) { if ( b ) fRemoveLogAfterSuccessCopy = true; fRemoveLogDirectoryAfterSuccessCopy = b; };

    void          RandomizeFilelist( bool b=true) { fRandomizeFilelist = b;}
    bool          IsFileNameValid ( string filename );
    int           TestZipFile ( string from,string to );

    static string           GetXrdType ( const string xrdfilename );
    static string           GetXrdHost ( const string xrdfilename );
    static string           GetXrdPort ( const string xrdfilename );
    static string           GetXrdFilename ( const string xrdfilename );
    string                  GetXrdGRPath ( const string xrdfilename );
    
protected:

    // loging part
    XrdDownloadManagerLog   *fLogDM;
    bool          fDoXrdcpInLogFile;
    string          fLogDirectory;
    string          fCurrentLogFileName;
    string          fCurrentGlobalLogFileName;
    bool          fRemoveLogAfterSuccessCopy;
    bool          fRemoveLogDirectoryAfterSuccessCopy;
    bool          fRandomizeFilelist;

    // filenama part
    string          fFileNameFrom;
    string          fFileNameTo;
    bool          fDoAuthenticate;
    vector<string>      fListOfFileNamesFrom;
    vector<string>      fListOfFileNamesFromGR;
    string          fSourceTypes;
    string          fGlobalRedirectorName;


    virtual int       SetConfigOptions ( string var, string val );
    virtual int       FillListOfFileNamesFrom ( string fname );
    virtual EInputTypes_t   GetInputTypeId(string inputTypeString="std");
private:

    // commands path
    string          fXrdcpCmd;
    string          fXrdcpCmdOptions;
    string          fXrdcpCmdCn;
    string          fXrdcpCmdRoot;
    string          fXrdcpCmdRootMacro;

    bool          fDoZipTest;
    string          fZipCmd;

    // private log functions
    int           InitLogFile();
    void          RemovingCopyProcessLogFile();
    void          PrintListOfFileNamesFrom();
    
    void          GenerateRandomNumbers(unsigned int* num, const unsigned int size);
//     void            FindAndReplaceString ( string &source, const string find, string replace );

};

#endif // __XrdDownloadManagerBase_H__
// kate: indent-mode cstyle; space-indent on; indent-width 4; 
